#include "displacementchart.h"
#include "config/constants.h"

DisplacementChart::DisplacementChart(QCustomPlot* _customPlot) : BaseChart(_customPlot), yAxisCounter(0)
{
    initChart();
}

void DisplacementChart::setDisplaceData(QSharedPointer<QVector<float> > _pDisplace)
{
    this->pDisplace = _pDisplace;
}

void DisplacementChart::updateChart(const float &y)
{


    yXias.push_back(y);
    xXias.push_back(yAxisCounter);
    customPlot->graph(0)->setData(xXias, yXias);

    customPlot->replot();

    if(yAxisCounter > 300)
    {
//        xXias.removeFirst();
//        yXias.removeFirst();
        //设置X轴坐标范围
        customPlot->xAxis->setRange(xXias[xXias.size() - 299], yAxisCounter);
        //设置Y轴坐标范围
        customPlot->yAxis->setRange(-500,500);
    }



    //customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    yAxisCounter++;

    LOG_INFO << "======================yAxisCounter = " << yAxisCounter;
}

void DisplacementChart::resetAxis()
{
    yAxisCounter = 0;
    yXias.clear();
    xXias.clear();

    //设置X轴坐标范围
    customPlot->xAxis->setRange(0,300);
    //设置Y轴坐标范围
    customPlot->yAxis->setRange(-500,500);
    customPlot->replot();
    //customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

void DisplacementChart::initChart()
{
    //添加图形
    customPlot->addGraph();
//    customPlot->addGraph();

    //设置X轴坐标范围
    customPlot->xAxis->setRange(0,300);
    //设置Y轴坐标范围
    customPlot->yAxis->setRange(-500,500);

    //设置画笔
    customPlot->graph(0)->setPen(QPen(Qt::red));

    customPlot->graph(0)->setName("负荷位移曲线");
//    customPlot->graph(1)->setName("1111");

    //设置X轴文字标注
    customPlot->xAxis->setLabel("时间(ms)");
    //设置Y轴文字标注
    customPlot->yAxis->setLabel("位移(mm)");

//    customPlot->graph(0)->setBrush(QBrush(QColor(255,255,0)));
    customPlot->graph(0)->setBrush(QBrush(QColor(255,255,255)));



//    QVector<double> temp(20);
//    QVector<double> temp1(20);
//    //图形为y = 100*x;
//    for(int i=0;i<20;i++)
//    {
//        temp[i] = i;
//        temp1[i] = 10*i+10;
//    }

    customPlot->graph(0)->setData(xXias, yXias);

    //重绘 每次改变完以后都要调用这个进行重新绘制
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
